package com.baiyi.homeui.hshomeui.manage;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.baiyi.service.serialservice.serialdataservice.IOnSerialDataCallback;
import com.baiyi.service.serialservice.serialdataservice.ISerialControl;
import com.baiyi.service.serialservice.serialdataservice.data.entity.SerialData;

/**
 * Created by Administrator on 2023-09-25.
 */

public class SerialDataManage {

    public String TAG = "SerialDataManage";
    private static SerialDataManage instance;
    private ISerialControl mISerialControl;
    private boolean mConnected;
    private Context mContext;
    public static SerialDataManage getInstance() {
        if(instance==null){
            instance = new SerialDataManage();
        }
        return instance;
    }
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG,"onServiceConnected--");
            mISerialControl = ISerialControl.Stub.asInterface(service);
            mConnected = true;
            mservice = service;

            if(onServicesConnected!=null){
                onServicesConnected.connected();
            }
            /*JSONObject object = new JSONObject();
            try {
                object.put("devicesType","xxx");
                object.put("OS","Android");
                object.put("productId","xxx");
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
            SerialData serialData = new SerialData();
            serialData.setVersion("1");
            serialData.setCmdType(200+"");
            serialData.setDataContent(object.toString());

            sendMsg(serialData);*/

            /*SerialData serialData = new SerialData();
            serialData.setVersion("1");
            serialData.setCmdType(200+"");
            sendMsg(serialData);*/
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG,"onServiceDisconnected--");
            mISerialControl = null;
            mConnected = false;

        }
    };


    private IBinder mservice;

    public IBinder getMservice() {
        return mservice;
    }

    /*初始化*/
    public void init(Context context){
        this.mContext = context;
        Intent intent = new Intent();
        intent.setClassName("com.baiyi.service.serialservice.serialdataservice",
                "com.baiyi.service.serialservice.serialdataservice.SerialService");
        mContext.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

    }

    public void sendMsg(SerialData data,int ackNum){
        Log.d(TAG,"sendMsg--"+data.toString());
        if(mISerialControl==null){
            return ;
        }
        try {
            mISerialControl.sendData(data,ackNum);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void setOnDataChangeCallback(IOnSerialDataCallback callback){
        Log.d(TAG,"setOnDataChangeCallback--"+mISerialControl+"--callback--"+callback);
        if(mISerialControl==null){
            return ;
        }
        try {
            mISerialControl.onSerialDataChange(callback);

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private OnServicesConnected onServicesConnected;

    public void setOnServicesConnected(OnServicesConnected onServicesConnected) {
        this.onServicesConnected = onServicesConnected;
    }

    public interface OnServicesConnected{
        void connected();
    }



}
